package com.example.demo.controller;

import com.example.demo.pojo.*;
import com.example.demo.service.UService;
import com.example.demo.service.UserService;
import com.example.demo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/userLogin")
    public Result UserLogin(@RequestBody User user)
    {
        User e=userService.login(user);
        if(e!=null){
            Map<String, Object> Claims=new HashMap<>();
            Claims.put("id",e.getUID());
            Claims.put("username",e.getUName());
            String jwt = JwtUtils.generateJwt(Claims);
            e.setJwt(jwt);
            return Result.success(e);
        }
        return Result.error("-1");
    }
    @PostMapping("/userAdd")
    public Result UserAdd(@RequestBody User user)
    {

        log.info("userID为{}",user.getUID());
        log.info("userName为{}",user.getUName());
        userService.AddUser(user);

        return Result.success();
    }

    @PostMapping("/userUpdate")
    public Result userUpdate(@RequestBody User user)
    {
        userService.userUpd(user);
        return Result.success();
    }
    @GetMapping("/allWeb")
    public Result allWeb()
    {
        List<Web> webList=userService.allWeb();
        return Result.success(webList);
    }
    @PostMapping("/addWeb")
    public Result addWeb(@RequestBody Web web)
    {
        userService.addWeb(web);
        return Result.success();
    }
    @PostMapping("/selectWeb")
    public Result selectWeb(@RequestBody SelectWeb selectWeb)
    {
        List<Web>webList=userService.selectWeb(selectWeb);
        return Result.success(webList);
    }
    @PostMapping("/findWeb")
    public Result findWeb(@RequestBody Web web)
    {
        log.info("收到消息{}",web.getWebUrl());
        Web webList=userService.findWeb(web);
        if(webList != null)
            return Result.success(webList.getPhishing());
        else
            return Result.error("-1");
    }
}
